package com.springCloud.Prototype;

/**
 * @ClassName Stu
 * @Author 1628994918qq.com
 * @Date 2023/3/31 19:15
 * 原型模式的核心思想是通过复制已有对象的属性来创建新的对象，而不是通过构造函数来创建。
 * 为了实现这个功能，我们需要将需要克隆的对象实现 Cloneable 接口，并重写其中的 clone() 方法。
 */
public class Stu implements Cloneable {
    private String name;

    private int password;

    public Stu(String name, int password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPassword() {
        return password;
    }

    public void setPassword(int password) {
        this.password = password;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Stu stu1 = new Stu("yyyy",123);
        Stu stu2 = (Stu) stu1.clone();

        System.out.println("stu1：姓名:" + stu1.getName() + "密码" + stu1.getPassword());
        System.out.println("stu2：姓名:" + stu2.getName() + "密码" + stu2.getPassword());

        System.out.println("============");
        System.out.println(stu1);
        System.out.println(stu2);
        stu2.setName("ssss");
        stu2.setPassword(321);

        System.out.println("stu1：姓名:" + stu1.getName() + "密码" + stu1.getPassword());
        System.out.println("stu2：姓名:" + stu2.getName() + "密码" + stu2.getPassword());
    }
}
